package com.atguigu.gmall.mq.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author atguigu-mqx
 */
@RestController
@RequestMapping("mq")
public class MqController {

    @Autowired
    private RabbitService rabbitService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    //  发送消息！
    @GetMapping("sendMsg")
    public Result sendMsg(){
        //  调用发送消息的方法
        rabbitService.sendMsg("exchange.confirm","routing.confirm666","死鬼,出来接客了.");
        return Result.ok();
    }

    //  测试发送延迟消息
    @GetMapping("sendDeadLettle")
    public Result sendDeadLettle(){
        //  设置一下时间格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //  发送消息
        rabbitService.sendMsg(DeadLetterMqConfig.exchange_dead,DeadLetterMqConfig.routing_dead_1,"ok");
        System.out.println(simpleDateFormat.format(new Date())+":\t sendMsg");
        //  返回数据
        return Result.ok();
    }

    @GetMapping("sendDelay")
    public Result sendDealy(){
        //  设置一下时间格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //  发送消息：
        this.rabbitTemplate.convertAndSend(DelayedMqConfig.exchange_delay,DelayedMqConfig.routing_delay,"ok---",(message)->{
            //  利用消息对象设置延迟时间
            message.getMessageProperties().setDelay(10000);
            System.out.println(simpleDateFormat.format(new Date())+":\t sendMsg");
            return message;
        });
        //  返回数据
        return Result.ok();
    }

}
